package com.swgk.core.view.attr;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;
import androidx.databinding.BindingAdapter;

import com.aliyun.vod.common.utils.DeviceUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.request.RequestOptions;
import com.swgk.core.customview.GlideCircleTransform;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * Description:
 * Author zhengkewen
 * Time
 */
public class ImageViewAttrAdapter {

    @BindingAdapter(value = {"imageId"}, requireAll = false)
    public static void loadRoundImageId(ImageView imageView, int imageId) {
        RequestOptions options = new RequestOptions()
                .centerCrop();
        Glide.with(imageView.getContext())
                .load(imageId)
                .apply(options)
                .into(imageView);
    }

    @BindingAdapter(value = {"imageUrlLocal", "imageId"}, requireAll = false)
    public static void loadImageUrlOrLocal(ImageView imageView, String url, int imageId) {
        RequestOptions options = new RequestOptions()
                .dontAnimate()
                .placeholder(imageId)
                .error(imageId)
                .centerCrop();
        Glide.with(imageView.getContext())
                .load(url)
                .apply(options)
                .into(imageView);
    }


//    @BindingAdapter(value = {"imageUrlRound", "imageIdRound"}, requireAll = false)
//    public static void loadImageRound(ImageView imageView, String url, int imageId) {
//        RequestOptions options = new RequestOptions()
//                .dontAnimate()
//                .placeholder(imageId)
//                .error(imageId)
//                .transform(new GlideRoundTransform(imageView.getContext(), 8));
//        Glide.with(imageView.getContext())
//                .load(url)
//                .apply(options)
//                .into(imageView);
//    }

    @BindingAdapter(value = {"imageUrlCircle", "imageIdCircle"}, requireAll = false)
    public static void loadImageCircle(ImageView imageView, String url, int imageId) {
        RequestOptions options = new RequestOptions()
                .dontAnimate()
                .placeholder(imageId)
                .error(imageId)
                .transform(new GlideCircleTransform());
        Glide.with(imageView.getContext())
                .load(imageId)
                .apply(options)
                .into(imageView);
    }

    @BindingAdapter(value = {"roundImageUrl", "roundEmptyImage", "roundSize"}, requireAll = false)
    public static void loadRoundImageUrlOrLocal(ImageView imageView, String roundImageUrl, @DrawableRes int roundEmptyImage, int roundSize) {
        Context context = imageView.getContext();
        //图片变换
        MultiTransformation<Bitmap> transforms = new MultiTransformation<>(new CenterCrop(),
                new RoundedCornersTransformation(DeviceUtils.dip2px(context, roundSize == 0 ? 10 : roundSize), 0));
        //glide配置
        RequestOptions options = new RequestOptions()
                .dontAnimate()
                .transform(transforms);
        //加载异常展位图
        RequestBuilder<Drawable> empty = Glide.with(context).load(roundEmptyImage).apply(options);
        //正常加载图片
        Glide.with(context)
                .load(roundImageUrl)
                .apply(options)
                .thumbnail(empty)
                .error(empty)
                .into(imageView);
    }
}
